package service.impl;

import dao.SysUrlDao;
import model.SysUrl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import service.SysUrlService;
import util.MessageUtil;
import util.ReturnResultUtil;
import view.QueryView;
import view.UrlView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gable on 2018/5/22.
 */
@Service
public class SysUrlServiceImpl implements SysUrlService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private SysUrlDao sysUrlDao;

    @Override
    public ReturnResultUtil queryUrl(QueryView queryView) {
        List<UrlView> list = sysUrlDao.queryUrl(queryView);
        List<UrlView> returnList = new ArrayList<>();
        //获取一级菜单
        for(UrlView url : list){
           if(url.getLevel() == 0){
               returnList.add(url);
           }
        }
        //二级菜单
        for(UrlView urlView : returnList){
            List<SysUrl> subList = new ArrayList<>();
            for(SysUrl url : list){
                if(url.getUpper() != null && urlView.getId().intValue() == url.getUpper().intValue()){
                    subList.add(url);
                }
            }
            if(!CollectionUtils.isEmpty(subList)){
                urlView.setList(subList);
            }
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,returnList);
    }
}
